<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <button onclick="buttonClick()">执行</button>
    <button onclick="buttonClickImme()">立即执行</button>
</body>
<script>
    function debounce(fn,delay,immediate = false){
        let timer = null
        return function(...attr){
            if(timer) clearTimeout(timer)
            if(immediate){
                let callNow = !timer
                timer = setTimeout(() => {
                    timer = null
                    if(!callNow) fn.apply(this,attr)
                },delay)
                if(callNow) fn.apply(this,attr)
            } else {
                timer = setTimeout(() => {
                    fn.apply(this,attr)
                },delay)
            }
        }
    }

    let buttonClick = debounce(()=>{
        console.log(new Date().getTime())
    },1000)
    let buttonClickImme = debounce(()=>{
        console.log(new Date().getTime())
    },1000,true)
</script>
</html>